﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TA.JSON;
using TA.Turbo.Controls;
using System.Collections;
using TA.DotNetUtils;

namespace TA.Turbo
{
	/// <summary>
	/// Contains extensions methods for <see cref="TA.JSON.JsonWriter"/> class.
	/// </summary>
	public static class _JsonWriterExtensions
	{
		/// <summary>
		/// Serializes an object using the specified <see cref="PropertyPair"/> collection.
		/// </summary>
		/// <param name="This"></param>
		/// <param name="obj"></param>
		/// <param name="properties"></param>
		public static void Serialize(this JsonWriter This, object obj, IEnumerable<PropertyPair> properties)
		{
			if (obj == null)
			{
				This.Null();
			}
			else if (properties.Any())
			{
				if (obj is IEnumerable)
				{
					This.StartArray();

					foreach (object childObj in ((IEnumerable)obj))
					{
						This.Serialize(childObj, properties);
					}

					This.EndArray();
				}
				else
				{
					This.StartObject();

					//if (obj is IPropertiesContainer)
					//{
					//  IPropertiesContainer pObj = (IPropertiesContainer)obj;

					//  foreach (PropertyPair propPair in properties)
					//  {
					//    This.PropertyName(propPair.JSONProperty);
					//    This.Serialize(obj.Eval(propPair.ObjectProperty), propPair.Children);
					//  }
					//}
					//else
					//{
					foreach (PropertyPair propPair in properties)
					{
						This.PropertyName(propPair.JSONProperty);
						This.Serialize(obj.Eval(propPair.ObjectProperty), propPair.Children);
					}
					//}

					This.EndObject();
				}
			}
			else
			{
				This.Value(obj);
			}
		}
	}
}
